﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace DeepCompare
{
    /// <summary>
    ///     Statically extensions to the <see cref="Type" /> type.
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        ///     Determines whether the given type is an <see cref="IEnumerable" /> type, which are array, generic lists, generic enumerables etc. So, checks wherever the type instance is a subset of some elements.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        ///     <c>true</c> if [is I enumerable type] [the specified type]; otherwise, <c>false</c>.
        /// </returns>
        public static bool ContainsSetOfElements(this Type type)
        {
            return typeof(IEnumerable).IsAssignableFrom(type);
        }

        /// <summary>
        ///     Gets the base types of the given type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static Type[] GetBaseTypes(this Type type)
        {
            var typeList = new List<Type>();
            var currentType = type;
            while(currentType != typeof(object))
            {
                typeList.Add(currentType);
                currentType = currentType.BaseType;
            }
            typeList.Add(typeof(object));
            return typeList.ToArray();
        }

        /// <summary>
        ///     Gets the default value for the given type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static object GetDefaultValue(this Type type)
        {
            return DefaultGenerator.GetDefaultValue(type);
        }
    }
}